package com.tianma315.core.product.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tianma315.core.attr.domain.AttrFieldDO;
import com.tianma315.core.attr.service.AttrFieldService;
import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.exception.IFastException;
import com.tianma315.core.common.type.EnumErrorCode;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.core.product.dao.ProductDao;
import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.domain.ProductFieldDO;
import com.tianma315.core.product.service.ProductFieldService;
import com.tianma315.core.product.service.ProductService;
import com.tianma315.core.product.vo.ProductFieldVO;
import com.tianma315.core.product.vo.ProductVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <pre>
 * 产品
 * </pre>
 * <small> 2019-08-06 14:41:19 | Wen</small>
 */
@Service
public class ProductServiceImpl extends CoreServiceImpl<ProductDao, ProductDO> implements ProductService {

    @Autowired
    private ProductFieldService productFieldService;

    @Autowired
    private AttrFieldService attrFieldService;

    @Autowired
    private FileService fileService;

    @Override
    public Page<ProductDO> getProductPage(Integer pageNumber, Integer pageSize, ProductDO productDTO) {
        // 查询列表数据
        Page<ProductDO> page = new Page<>(pageNumber, pageSize);

        Wrapper<ProductDO> wrapper = new EntityWrapper<ProductDO>();
        wrapper.ne("status",3);
//        wrapper.eq("merchant_id",productDTO.getMerchantId());
        wrapper.like("product_name",productDTO.getProductName());
        page = selectPage(page, wrapper);
        int total = selectCount(wrapper);
        page.setTotal(total);
        return page;
    }

    @Transactional
    @Override
    public Boolean addProduct(ProductVO productVO) {
        ProductDO productDO = new ProductDO();
        BeanUtils.copyProperties(productVO,productDO);
        if (productVO.getFile()!=null && !productVO.getFile().isEmpty()){
            try {
                 String url = fileService.upload(productVO.getFile().getBytes(),productVO.getFile().getOriginalFilename());
                productDO.setProductImg(url);
            } catch (IOException e) {
                System.out.println(e.getMessage());
                throw new IFastException(EnumErrorCode.FileUploadError.getCodeStr());
            }
        }

        productDO.setStatus(1);
        Boolean result = insert(productDO);


        List<ProductFieldDO> productFieldDOList = productVO.getProductFieldDOList();
        if (!CollectionUtils.isEmpty(productFieldDOList)){
            productFieldDOList.stream().forEach(list->{
                list.setState(StateEnum.not_del.getCode());
                list.setProductId(productDO.getProductId());
                productFieldService.insert(list);
            });
            /*for (ProductFieldDO productFieldDO:productFieldDOList) {
                productFieldDO.setState(StateEnum.not_del.getCode());
                productFieldDO.setProductId(productDO.getProductId());
                productFieldService.insert(productFieldDO);
            }*/
        }


        return result;
    }

    @Transactional
    @Override
    public Boolean updateProduct(ProductVO productVO) {
        ProductDO productDO = new ProductDO();
        BeanUtils.copyProperties(productVO,productDO);
        if (productVO.getFile()!=null && !productVO.getFile().isEmpty()){
            try {
                String url = fileService.upload(productVO.getFile().getBytes(),productVO.getFile().getOriginalFilename());
                productDO.setProductImg(url);
            } catch (IOException e) {
                System.out.println(e.getMessage());
                throw new IFastException(EnumErrorCode.FileUploadError.getCodeStr());
            }
        }
        Boolean result = updateById(productDO);

        List<ProductFieldDO> productFieldDOList = productVO.getProductFieldDOList();
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("product_id",productDO.getProductId());
        ProductFieldDO oldProductFieldDO = new ProductFieldDO();
        oldProductFieldDO.setState(StateEnum.is_del.getCode());
        productFieldService.update(oldProductFieldDO,wrapper);
        if (!CollectionUtils.isEmpty(productFieldDOList)){
            for (ProductFieldDO productFieldDO:productFieldDOList) {
                productFieldDO.setState(StateEnum.not_del.getCode());
                productFieldDO.setProductId(productDO.getProductId());
                productFieldService.insert(productFieldDO);
            }
        }

        return null;
    }

    @Override
    public Boolean updateStatus(Integer id, Integer status) {
        ProductDO productDO = new ProductDO();
        productDO.setStatus(status);
        productDO.setProductId(id);
        Boolean result = updateById(productDO);
        return result;
    }

    @Transactional
    @Override
    public Boolean deleteProduct(Integer id) {
        ProductDO productDO = new ProductDO();
        productDO.setProductId(id);
        productDO.setStatus(3);//3:删除
        Boolean result = updateById(productDO);

        EntityWrapper<ProductFieldDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",id);
        ProductFieldDO productFieldDO = new ProductFieldDO();
        productFieldDO.setState(StateEnum.is_del.getCode());
        productFieldService.update(productFieldDO,wrapper);
        return result;
    }

    @Override
    public ProductVO getProductById(Integer id) {
        ProductDO productDO = selectById(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(productDO,productVO);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("product_id",id);
        wrapper.eq("state",StateEnum.not_del.getCode());
        List<ProductFieldDO> productFields = productFieldService.selectList(wrapper);
        List<ProductFieldVO> productFieldVOList = new ArrayList<>();
        for (ProductFieldDO productField : productFields){
            ProductFieldVO productFieldVO = new ProductFieldVO();
            AttrFieldDO attrFieldDO = attrFieldService.selectById(productField.getAttrFieldId());
            BeanUtils.copyProperties(productField,productFieldVO);
            productFieldVO.setType(attrFieldDO.getType());
            productFieldVO.setAttrFieldName(attrFieldDO.getAttrFieldName());
            productFieldVOList.add(productFieldVO);
        }

        productVO.setProductFieldVOS(productFieldVOList);
        return productVO;
    }

    @Override
    public List<ProductDO> getProductDOList() {
        EntityWrapper<ProductDO> wrapper = new EntityWrapper();
        wrapper.eq("status",1);
//        wrapper.eq("merchant_id",merchantId);
        List<ProductDO> list = selectList(wrapper);
        return list;
    }
}
